package wms.util.redis;

import java.util.Objects;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * 配置redis
 * @description: 
 * @author 浮~沉
 * @date 2020年5月13日
 */
public class RedisAPI {
	
	//private StringRedisTemplate redisTemplate;
	
	private JedisPool jedisPool;

	public JedisPool getJedisPool() {
		return jedisPool;
	}

	public void setJedisPool(JedisPool jedisPool) {
		this.jedisPool = jedisPool;
	}
	
	
	/**
	 * set同时设置redis的存活时间
	 * @param key
	 * @param value
	 * @param time  过期时间
	 * @return
	 */
	public  boolean set(String key, Object value, Long time) {
		Jedis resource = jedisPool.getResource();
		if (key == null || value == null) {
			return false;
		}
		if (value instanceof String) {
			resource.set(key, String.valueOf(value));
		} /*else if (value instanceof List) {
			@SuppressWarnings({ "unchecked", "rawtypes" })
			List obj = (List)value;
			redisTemplate.opsForList().leftPushAll(key, obj);
		} else if (value instanceof Map) {
			@SuppressWarnings("rawtypes")
			Map map = (Map)value;
			redisTemplate.opsForHash().putAll(key, map);
		}
		
		if (time != null)
			redisTemplate.expire(key, time, TimeUnit.SECONDS);
		*/
		return true;
	}
	
	
	
	/**
	 * set String
	 * @param key
	 * @param value
	 * @return
	 */
	public String set(String key, String value) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.set(key, value);
		returnResources(jedisPool, jedis);
		return result;
	}
	
	/**
	 * set String 同时设置redis的存活时间
	 * @param key
	 * @param value
	 * @param time  过期时间
	 * @return
	 */
	public String set(String key, String value, Integer time) {
		Jedis jedis = jedisPool.getResource();
		String result = jedis.setex(key, time, value);
		returnResources(jedisPool, jedis);
		return result;
	}
	
	
	
	public boolean keyExist(String key) {
		Jedis jedis = jedisPool.getResource();
		Boolean flag = jedis.exists(key);
		returnResources(jedisPool, jedis);
		return flag;
	}
	
	public String getStringKey(String key) {
		Jedis jedis = jedisPool.getResource();
		String value = jedis.get(key);
		returnResources(jedisPool, jedis);
		return value;
	}
	
	
	
	@SuppressWarnings("deprecation")
	public static void returnResources(JedisPool jedisPool, Jedis jedis) {
		if (Objects.nonNull(jedisPool) && Objects.nonNull(jedis)) {
			System.out.println("redis归还连接....");
			jedisPool.returnResource(jedis);
		}
	}
	
	
	
	/*public Object getKey(String key) {
		return redisTemplate.opsForValue().get(key);
	}
	
	public String setObject(String key, Object object) {
		
	}
*/	
	
	public long ttl(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result = jedis.ttl(key);
		returnResources(jedisPool, jedis);
		return result;
	}
	
	
	
	/**
	 * 删除key
	 * @param key
	 * @return 返回删除的个数
	 */
	public Long delKey(String key) {
		Jedis jedis = jedisPool.getResource();
		Long result  = jedis.del(key);
		returnResources(jedisPool, jedis);
		return result;
	}
	


}
